<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\logic\CommonLogic;
use app\logic\PlayGoodsLogic;
use app\logic\ShopLogic;
use app\models\Region;
use Yii;


class IndexController extends BaseController
{

	public function actionIndex()
	{
        $this->response(time());
	}

    /**
     * 获取广告
     * @apiDescription   by Jim
     * @api              {post} /index/get-advertisement 获取广告
     * @apiName          get-advertisement
     * @apiVersion       1.0.0
     * @apiParam {int} type 类型：0去玩模块；1去买模块
     * @apiGroup         Index
     * @apiSampleRequest /index/get-advertisement?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
     *  `ad_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '广告位id',
     *  `title` varchar(255) NOT NULL COMMENT '广告标题',
     *  `pic` varchar(255) NOT NULL COMMENT '图片',
     *  `content` varchar(255) DEFAULT NULL COMMENT '图文内容',
     *  `position` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '广告位置：0去玩模块；1去买模块',
     *  `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
     * }
     * @apiError         none
     */

    public function actionGetAdvertisement()
    {
        $type = $this->getQueryparams('type',0);
        $commonLogic = new CommonLogic();
        $advertisement = $commonLogic->getAdvertisement($type);
        //$advertisement['pic'] = Yii::$app->params['upload']['upload_domain'].$advertisement['pic'];
        $this->response($advertisement);
    }
    /**
     * 获取具体广告
     * @apiDescription   by Jim
     * @api              {post} /index/get-adv-by-id 获取具体广告
     * @apiName          get-adv-by-id
     * @apiVersion       1.0.0
     * @apiParam {int} adv_id 广告id
     * @apiGroup         Index
     * @apiSampleRequest /index/get-adv-by-id?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
     *  `ad_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '广告位id',
     *  `title` varchar(255) NOT NULL COMMENT '广告标题',
     *  `pic` varchar(255) NOT NULL COMMENT '图片',
     *  `content` varchar(255) DEFAULT NULL COMMENT '图文内容',
     *  `position` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '广告位置：0去玩模块；1去买模块',
     *  `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
     * }
     * @apiError         none
     */
    public function actionGetAdvById()
    {
        $adv_id = $this->getQueryparams('adv_id',0);
        $commonLogic = new CommonLogic();
        $advertisement = $commonLogic->getAdvById($adv_id);
        //$advertisement['pic'] = Yii::$app->params['upload']['upload_domain'].$advertisement['pic'];
        $this->response($advertisement);
    }
//    /**
//     * 获取广告详情
//     * @apiDescription   by Jim
//     * @api              {post} /index/get-advertisement-detail 获取广告详情
//     * @apiName          get-advertisement-detail
//     * @apiVersion       1.0.0
//     * @apiParam {int} ad_id
//     * @apiGroup         Index
//     * @apiSampleRequest /index/get-advertisement-detail?flagsign=1
//     * @apiSuccessExample {json} 获取成功
//     * {
//     *  `ad_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '广告位id',
//     *  `title` varchar(255) NOT NULL COMMENT '广告标题',
//     *  `pic` varchar(255) NOT NULL COMMENT '图片',
//     *  `content` varchar(255) DEFAULT NULL COMMENT '图文内容',
//     *  `position` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '广告位置：0去玩模块；1去买模块',
//     *  `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
//     * }
//     * @apiError         none
//     */
    /*public function actionGetAdvertisementDetail()
    {
        $type = $this->getQueryparams('ad_id',0);
        $commonLogic = new CommonLogic();
        $advertisement = $commonLogic->getAdvertisementDetail(advertisment_id);
        $this->response($advertisement);
    }*/

    /**
     * 获取商店列表
     * @apiDescription   by Jim
     * @api              {post} /index/get-shop-list 获取商店列表
     * @apiName          get-shop-list
     * @apiVersion       1.0.0
     * @apiParam {string} region_code 精准的位置编码
     * @apiParam {int} tag_id 标签id，默认为0是全部
     * @apiParam {string} longitude 当前所在经纬度，供计算商店距离
     * @apiParam {string} latitude 当前所在经纬度，供计算商店距离
     * @apiGroup         Index
     * @apiSampleRequest /index/get-shop-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
     *  `shop_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商家id',
    `shop_no` varchar(255) NOT NULL COMMENT '商家编号',
    `address` varchar(255) NOT NULL COMMENT '店铺地址',
    `shop_name` varchar(255) NOT NULL COMMENT '商家名称',
    `shop_parent_name` varchar(255) NOT NULL COMMENT '店铺名称',
    `longitude` varchar(10) unsigned NOT NULL COMMENT '经度',
    `latitude` varchar(10) unsigned NOT NULL COMMENT '纬度',
    `shop_pic` varchar(255) NOT NULL COMMENT '商家图片',
    `shop_pics` text NOT NULL COMMENT '商家图片集',
    `description` text NOT NULL COMMENT '商家介绍',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
    `owner_user_id` int(11) unsigned NOT NULL COMMENT '店主id,取tbl_user表的id',
    `owner_user_name` varchar(255) NOT NULL,
    `contract_start_time` datetime DEFAULT NULL COMMENT '合同开始时间',
    `contract_end_time` datetime DEFAULT NULL COMMENT '合同结束时间',
    `contract_create_time` datetime DEFAULT NULL COMMENT '合同创建时间',
    `city_id` varchar(10) NOT NULL COMMENT '城市id',
    `district_id` varchar(10) NOT NULL COMMENT '区id',
    `street_id` varchar(10) NOT NULL COMMENT '街道id',
     distance 距离
     tag:[{
        `tag_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '标签id',
        `tag_name` varchar(255) NOT NULL COMMENT '标签名称',
        `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态：0无效，默认1有效',
     }]
     * }]
     * @apiError         none
     */

    public function actionGetShopList()
    {
        $region_code = $this->getQueryparams('region_code','');
        $tag_id = $this->getQueryparams('tag_id',0);
        $longitude = $this->getQueryparams('longitude','');
        $latitude = $this->getQueryparams('latitude','');
        //$page = $this->getQueryparams('page',1);
        //$limit = $this->getQueryparams('limit',20);
        //$skip = $limit*($page-1);
        $shopLogic = new ShopLogic();
        //$shopList = $shopLogic->getShopList($region_code,$tag_id,$skip,$limit);
        $shopList = $shopLogic->getShopList($region_code,$tag_id);
        $result = [];
        $final_result = [];
        foreach ($shopList AS $key=>&$value){
            $tag = $shopLogic->getTag($value['shop_id']);
            $value['tag'] = $tag;
            $distance = self::_getdistance($longitude,$latitude,$value['longitude'],$value['latitude']);
            if($distance>1000){
                if(($distance/1000)>10){
                    $value['distance'] = '>10km';
                }else{
                    $value['distance'] = round(($distance/1000)).'km';
                }
            }else{
                $value['distance'] = round($distance).'m';
            }
            $result[$distance][] = $value;
        }
        ksort($result);
        foreach($result AS $key=>$value){
            foreach($value AS $k=>$v){
                $final_result[] = $v;
            }
        }
        $this->response($final_result);
    }
    /**
     * 获取最近商店
     * @apiDescription   by Jim
     * @api              {post} /index/get-cloest-shop 获取最近商店
     * @apiName          get-cloest-shop
     * @apiVersion       1.0.0
     * @apiParam {string} longitude 当前所在经纬度，供计算商店距离
     * @apiParam {string} latitude 当前所在经纬度，供计算商店距离
     * @apiGroup         Index
     * @apiSampleRequest /index/get-cloest-shop?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    `shop_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商家id',
    `shop_no` varchar(255) NOT NULL COMMENT '商家编号',
    `zone_id` int(11) unsigned NOT NULL COMMENT '地址id',
    `address` varchar(255) NOT NULL COMMENT '店铺地址',
    `shop_name` varchar(255) NOT NULL COMMENT '商家名称',
    `shop_parent_name` varchar(255) NOT NULL COMMENT '店铺名称',
    `longitude` int(3) unsigned NOT NULL COMMENT '经度',
    `latitude` int(3) unsigned NOT NULL COMMENT '纬度',
    `shop_pic` varchar(255) NOT NULL COMMENT '商家图片',
    `shop_pics` text NOT NULL COMMENT '商家图片集',
    `description` text NOT NULL COMMENT '商家介绍',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
    `owner_user_id` int(11) unsigned NOT NULL COMMENT '店主id,取tbl_user表的id',
    `owner_user_name` varchar(255) NOT NULL,
    `contract_start_time` datetime DEFAULT NULL COMMENT '合同开始时间',
    `contract_end_time` datetime DEFAULT NULL COMMENT '合同结束时间',
    `contract_create_time` datetime DEFAULT NULL COMMENT '合同创建时间',
    `city_id` varchar(10) NOT NULL COMMENT '城市id',
    `district_id` varchar(10) NOT NULL COMMENT '区id',
    `street_id` varchar(10) NOT NULL COMMENT '街道id',
    distance 距离
     * }
     * @apiError         none
     */
    public function actionGetCloestShop(){
        //$region_code = $this->getQueryparams('region_code','');
        $longitude = $this->getQueryparams('longitude','');
        $latitude = $this->getQueryparams('latitude','');
        //$page = $this->getQueryparams('page',1);
        //$limit = $this->getQueryparams('limit',20);
        //$skip = $limit*($page-1);
        $shopLogic = new ShopLogic();
        //$shopList = $shopLogic->getShopList($region_code,$tag_id,$skip,$limit);
        $shopList = $shopLogic->getAllShop();
        $result = [];
        foreach ($shopList AS $key=>&$value){
            $distance = self::_getdistance($longitude,$latitude,$value['longitude'],$value['latitude']);
            if($distance>1000){
                if(($distance/1000)>10){
                    $value['distance'] = '>10km';
                }else{
                    $value['distance'] = round(($distance/1000)).'km';
                }

            }else{
                $value['distance'] = round($distance).'m';
            }

            $result[$distance][] = $value;
        }
        ksort($result);
        $result = array_values($result);
        if($result){
            $result = $result[0];
        }else{
            $this->response();
        }
        $this->response($result[0]);
    }

    private static function _getdistance($lng1, $lat1, $lng2, $lat2) {
        // 将角度转为狐度
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        return $s;
    }


    /**
     * 平台介绍
     * @apiDescription   by Jim
     * @api              {post} /index/get-article 平台介绍
     * @apiName          get-article
     * @apiVersion       1.0.0
     * @apiParam {int} article_id 文章id 默认传1，取第一个
     * @apiGroup         Index
     * @apiSampleRequest /index/get-article?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    `article_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '文章id',
    `article_name` varchar(255) NOT NULL COMMENT '文章标题',
    `content` text COMMENT '内容',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态，默认1有效，0无效',
     * }
     * @apiError         none
     */
    public function  actionGetArticle(){
        $article_id = $this->getQueryparams('article_id',1);
        $commonLogic = new CommonLogic();
        $article = $commonLogic->getArticle($article_id);
        $this->response($article);
    }

    /**
     * 常见问题列表
     * @apiDescription   by Jim
     * @api              {post} /index/get-question 常见问题列表
     * @apiName          get-question
     * @apiVersion       1.0.0
     * @apiGroup         Index
     * @apiSampleRequest /index/get-question?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    `article_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '文章id',
    `article_name` varchar(255) NOT NULL COMMENT '文章标题',
    `content` text COMMENT '内容',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态，默认1有效，0无效',
     * }]
     * //拿到article_id使用/index/get-article接口调用文章内容
     * @apiError         none
     */
    public function  actionGetQuestion(){
        $commonLogic = new CommonLogic();
        $article = $commonLogic->getQuestion();
        $this->response($article);
    }




}